1 /*
2 Copyright: Marcelo S. N. Mancini (Hipreme|MrcSnm), 2018 - 2021
3 License:   [https://creativecommons.org/licenses/by/4.0/|CC BY-4.0 License].
4 Authors: Marcelo S. N. Mancini
5 
6 	Copyright Marcelo S. N. Mancini 2018 - 2021.
7 Distributed under the CC BY-4.0 License.
8    (See accompanying file LICENSE.txt or copy at
9 	https://creativecommons.org/licenses/by/4.0/
10 */
11 
12 module hip.api.input.button;
13 
14 enum HipButtonType : ushort
15 {
16     down,
17     up
18 }
19 
20 import std.typecons:Flag;
21 
22 alias AutoRemove = Flag!"AutoRemove";
23 alias HipInputAction = void delegate(const(AHipButtonMetadata) meta);
24 alias HipTouchMoveAction = void delegate(int x, int y);
25 alias HipScrollAction = void delegate(float[3]);
26 
27 /** 
28  * Handler for any kind of button
29  */
30 struct HipButton
31 {
32     ushort id;
33     HipButtonType type;
34     HipInputAction action;
35     AutoRemove isAutoRemove;
36 
37     alias id this;
38 }
39 
40 struct TouchMoveListener
41 {
42     HipTouchMoveAction action;
43     AutoRemove isAutoRemove;
44 }
45 
46 struct ScrollListener
47 {
48     HipScrollAction action;
49     AutoRemove isAutoRemove;
50 }
51 
52 abstract class AHipButtonMetadata
53 {
54     int id;
55     this(int id){this.id = id;}
56     public abstract float getDownTimeDuration() const;
57     public abstract float getLastDownTimeDuration() const;
58     public abstract float getUpTimeDuration() const;
59     public abstract float getLastUpTimeDuration() const;
60     public abstract bool isPressed() const;
61     ///Useful for looking justPressed and justRelease
62     public abstract bool isNewState() const;
63     ///User API is not expected to call that function. It is used for controlling the input flow
64     public abstract void setPressed(bool press);
65     public final bool isJustReleased() const {return !isPressed && isNewState;}
66     public final bool isJustPressed()const {return isPressed && isNewState;}
67 }